package com.easepal7506a.project.ui.activity.activity_7506l;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.Group;

import com.easepal7506a.project.Constant.CommmandNum;
import com.easepal7506a.project.R;
import com.easepal7506a.project.interfaces.IProMasaChoose;
import com.easepal7506a.project.manager.DataManager;
import com.easepal7506a.project.observer.CloseProfessionalObserver;
import com.easepal7506a.project.observer.TimeObserver;
import com.easepal7506a.project.ui.activity.BaseActivity;
import com.easepal7506a.project.ui.iview.IProfessionalMassageView7506;
import com.easepal7506a.project.ui.presenter.presenter_7506l.ProfessionalMassagePresenter7506l;
import com.easepal7506a.project.utils.ActivityUtils;
import com.easepal7506a.project.view.VerticalViewPager.VerticalAdapter;
import com.easepal7506a.project.view.VerticalViewPager.VerticalViewPager;
import com.easepal7506a.project.widget.LayoutGzsy_7506;
import com.easepal7506a.project.widget.LayoutQjsd_7506;
import com.easepal7506a.project.widget.LayoutRqzs_7506;
import com.easepal7506a.project.widget.LayoutShfs_7506;
import com.easepal7506a.project.widget.LayoutSpa_7506;
import com.easepal7506a.project.widget.LayoutStjc_7506;
import com.easepal7506a.project.widget.LayoutZtms_7506;
import com.example.reslib.utils.DisplayUtil;
import com.example.reslib.utils.PreferenceWrapper;
import com.ogawa.base.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 专业按摩
 */
public class ProfessionalMassageActivity7506L extends BaseActivity implements View.OnClickListener, IProMasaChoose, IProfessionalMassageView7506,
        CloseProfessionalObserver.onCloseProLis, TimeObserver.onTimeUpdataLis {
    private LayoutStjc_7506 mPageStjc;//酸痛检测
    private LayoutZtms_7506 mPageZtms;//主题模式
    private LayoutSpa_7506 mPageSpa;//特色spa
    private LayoutQjsd_7506 mPageQjsd;//情景时段
    private LayoutRqzs_7506 mPageRqzs;//人群专属
    private LayoutGzsy_7506 mPageGzsy;//工作释压
    private LayoutShfs_7506 mPageShfs;//生活放松
    private VerticalAdapter mAdapter;
    private VerticalViewPager mVerticalViewPager;

    private List<View> mVerticalViews;
    private ImageView mIvPointStjc;//酸痛检测
    private ImageView mIvPointZtms;//主题模式
    private ImageView mIvPointSpa;//特色spa
    private ImageView mIvPointQjsd;//情景时段
    private ImageView mIvPointRqzs;//人群专属
    private ImageView mIvPointGzsy;//工作释压
    private ImageView mIvPointShfs;//生活放松

    private TextView mTvNameStjc;//酸痛检测
    private TextView mTvNameZtms;//主题模式
    private TextView mTvNameSpa;//特色spa
    private TextView mTvNameQjsd;//情景时段
    private TextView mTvNameRqzs;//人群专属
    private TextView mTvNameGzsy;//工作释压
    private TextView mTvNameShfs;//生活放松
    private List<ImageView> mPoints = new ArrayList<>();
    private List<TextView> mClassifys = new ArrayList<>();
    private ProfessionalMassagePresenter7506l mPresenter;
    private String mCurName;
    private String mCurCode;
    private int mChoose;


    @Override
    public void initTitleBar() {
        super.initTitleBar();
        titleBar.setTitleNameGone();
    }

    @Override
    public void initView(int layoutId) {
        super.initView(layoutId);
        Log.e("ProfessionalMass", "ProfessionalMassageActivity7506" + "===" + mCurName);
        PreferenceWrapper mSp = new PreferenceWrapper();
        mChoose = Integer.parseInt(mSp.getStringValue("app", "1"));
        Group groupSpa = findViewById(R.id.group_spa);
        if (mChoose == 4) {
            groupSpa.setVisibility(View.GONE);
        }
        mVerticalViews = new ArrayList<>();
        mCurCode = DataManager.getInst().getAuto();
        mPageStjc = (LayoutStjc_7506) LayoutInflater.from(this).inflate(R.layout.layout_pm_page_stjc_7506, null);
        mPageZtms = (LayoutZtms_7506) LayoutInflater.from(this).inflate(R.layout.layout_pm_page_ztms_7506, null);
        mPageSpa = (LayoutSpa_7506) LayoutInflater.from(this).inflate(R.layout.layout_pm_page_spa_7506, null);
        mPageQjsd = (LayoutQjsd_7506) LayoutInflater.from(this).inflate(R.layout.layout_pm_page_qjsd_7506, null);
        mPageRqzs = (LayoutRqzs_7506) LayoutInflater.from(this).inflate(R.layout.layout_pm_page_rqzs_7506, null);
        mPageGzsy = (LayoutGzsy_7506) LayoutInflater.from(this).inflate(R.layout.layout_pm_page_gzsy_7506, null);
        mPageShfs = (LayoutShfs_7506) LayoutInflater.from(this).inflate(R.layout.layout_pm_page_shfs_7506, null);

        mTvNameStjc = findViewById(R.id.tv_stjc);
        mTvNameZtms = findViewById(R.id.tv_ztms);
        mTvNameSpa = findViewById(R.id.tv_spa);
        mTvNameQjsd = findViewById(R.id.tv_qjsd);
        mTvNameRqzs = findViewById(R.id.tv_rqzs);
        mTvNameGzsy = findViewById(R.id.tv_gzsy);
        mTvNameShfs = findViewById(R.id.tv_shfs);

        mTvNameStjc.setOnClickListener(this);
        mTvNameZtms.setOnClickListener(this);
        mTvNameSpa.setOnClickListener(this);
        mTvNameQjsd.setOnClickListener(this);
        mTvNameRqzs.setOnClickListener(this);
        mTvNameGzsy.setOnClickListener(this);
        mTvNameShfs.setOnClickListener(this);

        mPageStjc.setAction(this);
        mPageZtms.setAction(this);
        mPageSpa.setAction(this);
        mPageQjsd.setAction(this);
        mPageRqzs.setAction(this);
        mPageGzsy.setAction(this);
        mPageShfs.setAction(this);


        mIvPointStjc = findViewById(R.id.pm_potin_stjc);
        mIvPointStjc.setSelected(true);
        mIvPointStjc.setSelected(true);
        mIvPointZtms = findViewById(R.id.pm_potin_ztms);
        mIvPointSpa = findViewById(R.id.pm_potin_spa);
        mIvPointQjsd = findViewById(R.id.pm_potin_qjsd);
        mIvPointRqzs = findViewById(R.id.pm_potin_rqzs);
        mIvPointGzsy = findViewById(R.id.pm_potin_gzsy);
        mIvPointShfs = findViewById(R.id.pm_potin_shfs);
        mPoints.clear();
        mPoints.add(mIvPointStjc);
        mPoints.add(mIvPointZtms);
        if (mChoose != 4) {
            mPoints.add(mIvPointSpa);
        }
        mPoints.add(mIvPointQjsd);
        mPoints.add(mIvPointRqzs);
        mPoints.add(mIvPointGzsy);
        mPoints.add(mIvPointShfs);
        mClassifys.clear();
        mClassifys.add(mTvNameStjc);
        mClassifys.add(mTvNameZtms);
        if (mChoose != 4) {
            mClassifys.add(mTvNameSpa);
        }
        mClassifys.add(mTvNameQjsd);
        mClassifys.add(mTvNameRqzs);
        mClassifys.add(mTvNameGzsy);
        mClassifys.add(mTvNameShfs);
        mVerticalViewPager = findViewById(R.id.verticalViewPager);
        mVerticalViews.add(mPageStjc);
        mVerticalViews.add(mPageZtms);
        if (mChoose != 4) {
            mVerticalViews.add(mPageSpa);
        }
        mVerticalViews.add(mPageQjsd);
        mVerticalViews.add(mPageRqzs);
        mVerticalViews.add(mPageGzsy);
        mVerticalViews.add(mPageShfs);
        mVerticalViewPager.setOnPageChangeListener(new OnPageChangeListenerImpl());
        mAdapter = new VerticalAdapter(mVerticalViews);
        mVerticalViewPager.setAdapter(mAdapter);
        mPresenter = new ProfessionalMassagePresenter7506l(this);
        mPresenter.initData();
        CloseProfessionalObserver.getInst().registerObserver(this);
        TimeObserver.getInst().registerObserver(this);
    }

    @Override
    public void chooseMassage(String name, String Code) {
        if (TextUtils.isEmpty(mCurName) || !mCurName.equals(name)) {
            if (Code.equals(CommmandNum.SUANTJZ)) {
                showAlert(name, Code);
            } else {
                sendCommond(Code);
            }

        } else {
            ActivityUtils.startProMasDetailActivityL(this, name);
        }


    }

    private void showAlert(String name, String Code) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_to_ache_check, null);
        dialogBuilder.setView(dialogView);
        TextView mTvCancle = dialogView.findViewById(R.id.dialog_cancle);
        TextView mTvOk = dialogView.findViewById(R.id.dialog_ok);


        final AlertDialog alertDialog = dialogBuilder.create();
        //加上下面两句才能显示圆角 不然圆角处会有白色效果
        Window window = alertDialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        alertDialog.setCancelable(false);
        alertDialog.show();
        mTvCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                alertDialog.cancel();
            }
        });
        mTvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mCurName) || !mCurName.equals(name)) {
                    sendCommond(Code);
                } else {
                    ActivityUtils.startProMasDetailActivityL(ProfessionalMassageActivity7506L.this, name);
                }
                alertDialog.cancel();
            }
        });
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (DisplayUtil.getScreenWidthPx(this) * 0.9); //设置宽度
        window.setAttributes(lp);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestory();
        CloseProfessionalObserver.getInst().removeObserver(this);
        TimeObserver.getInst().removeObserver(this);
    }

    @Override
    public void noticeChange(int page, int position) {
        makaChoose(page, position);
    }

    @Override
    public void closeActivity() {
        CloseProfessionalObserver.getInst().noticeonClose();
    }

    @Override
    public void startAdvance() {
        if (CommonUtil.isActivityTop(ProfessionalMassageActivity7506L.class, this)) {
            ActivityUtils.startAdvancedMassageActivity(this);
            this.finish();
        }

    }

    @Override
    public void startDetail(String name) {
        if (CommonUtil.isActivityTop(ProfessionalMassageActivity7506L.class, this)) {
            ActivityUtils.startProMasDetailActivityL(this, name);
        }
    }


    @Override
    public int setLayoutId() {
        return R.layout.activity_professional_massage_7506_l;
    }


    @Override
    public void getName(String name) {
        mCurName = name;
    }


    @Override
    public void onCloseActivity() {
        this.finish();

    }

    @Override
    public void onupdataTime(String min, String sec) {
        doUpDataTime(min, sec);
    }

    @Override
    public void timeFinish() {
        super.timeFinish();
        if (CommonUtil.isActivityTop(ProfessionalMassageActivity7506L.class, this)) {
            showFinishDialog(this, "0", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancleFinishDialog();
                    ActivityUtils.startHomeActivity(ProfessionalMassageActivity7506L.this);
                    ProfessionalMassageActivity7506L.this.finish();
                }
            });
        }
    }

    @Override
    public void isPause(boolean isPause) {
        if (isPause) {
            pauseTime();
        } else {
            reStarTime();
        }
    }

    class OnPageChangeListenerImpl implements VerticalViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            for (int i = 0; i < mPoints.size(); i++) {
                if (position == i) {
                    mPoints.get(i).setSelected(true);
                    mClassifys.get(i).setSelected(true);
                } else {
                    mPoints.get(i).setSelected(false);
                    mClassifys.get(i).setSelected(false);
                }
            }

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }


    @Override
    public void onClick(View v) {
        int offset = mChoose == 4 ? 1 : 0;
        int i = v.getId();
        if (i == R.id.tv_stjc) {
            mVerticalViewPager.setCurrentItem(0);

        } else if (i == R.id.tv_ztms) {
            mVerticalViewPager.setCurrentItem(1);

        } else if (i == R.id.tv_spa) {
            mVerticalViewPager.setCurrentItem(2);

        } else if (i == R.id.tv_qjsd) {

            mVerticalViewPager.setCurrentItem(3 - offset);

        } else if (i == R.id.tv_rqzs) {
            mVerticalViewPager.setCurrentItem(4 - offset);

        } else if (i == R.id.tv_gzsy) {
            mVerticalViewPager.setCurrentItem(5 - offset);

        } else if (i == R.id.tv_shfs) {
            mVerticalViewPager.setCurrentItem(6 - offset);

        }

    }


    private void makaChoose(int page, int position) {
        int offset = mChoose == 4 ? 1 : 0;
        if (page == 0) {
            mPageStjc.setSelect(position);
            mPageZtms.setSelect(-1);
            mPageSpa.setSelect(-1);
            mPageQjsd.setSelect(-1);
            mPageRqzs.setSelect(-1);
            mPageGzsy.setSelect(-1);
            mPageShfs.setSelect(-1);
        } else if (page == 1) {
            mPageStjc.setSelect(-1);
            mPageZtms.setSelect(position);
            mPageSpa.setSelect(-1);
            mPageQjsd.setSelect(-1);
            mPageRqzs.setSelect(-1);
            mPageGzsy.setSelect(-1);
            mPageShfs.setSelect(-1);
        } else if (page == 2) {
            mPageStjc.setSelect(-1);
            mPageZtms.setSelect(-1);
            mPageSpa.setSelect(position);
            mPageQjsd.setSelect(-1);
            mPageRqzs.setSelect(-1);
            mPageGzsy.setSelect(-1);
            mPageShfs.setSelect(-1);

        } else if (page == 3) {
            mPageStjc.setSelect(-1);
            mPageZtms.setSelect(-1);
            mPageSpa.setSelect(-1);
            mPageQjsd.setSelect(position);
            mPageRqzs.setSelect(-1);
            mPageGzsy.setSelect(-1);
            mPageShfs.setSelect(-1);

        } else if (page == 4) {
            mPageStjc.setSelect(-1);
            mPageZtms.setSelect(-1);
            mPageSpa.setSelect(-1);
            mPageQjsd.setSelect(-1);
            mPageRqzs.setSelect(position);
            mPageGzsy.setSelect(-1);
            mPageShfs.setSelect(-1);

        } else if (page == 5) {
            mPageStjc.setSelect(-1);
            mPageZtms.setSelect(-1);
            mPageSpa.setSelect(-1);
            mPageQjsd.setSelect(-1);
            mPageRqzs.setSelect(-1);
            mPageGzsy.setSelect(position);
            mPageShfs.setSelect(-1);

        } else if (page == 6) {
            mPageStjc.setSelect(-1);
            mPageZtms.setSelect(-1);
            mPageSpa.setSelect(-1);
            mPageQjsd.setSelect(-1);
            mPageRqzs.setSelect(-1);
            mPageGzsy.setSelect(-1);
            mPageShfs.setSelect(position);

        } else {
            mPageStjc.setSelect(-1);
            mPageZtms.setSelect(-1);
            mPageSpa.setSelect(-1);
            mPageQjsd.setSelect(-1);
            mPageRqzs.setSelect(-1);
            mPageGzsy.setSelect(-1);
            mPageShfs.setSelect(-1);
        }
        if (page > 1) {
            mVerticalViewPager.setCurrentItem(page-1);
        } else {
            mVerticalViewPager.setCurrentItem(page);
        }

    }


}
